package com.weng.platform.article.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.constants.ResultCodeEnum;
import com.weng.framework.core.exception.GlobalServiceException;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import com.weng.framework.dao.mybatis.service.BaseServiceImpl;
import com.weng.platform.article.entity.ArticleContent;
import com.weng.platform.article.entity.ArticleInfo;
import com.weng.platform.article.mapper.ArticleContentMapper;
import com.weng.platform.article.mapper.ArticleInfoMapper;
import com.weng.platform.article.model.request.ArticleInfoReq;
import com.weng.platform.article.model.vo.ArticleInfoDetailVo;
import com.weng.platform.article.model.vo.ArticleInfoEditVo;
import com.weng.platform.article.service.IArticleInfoService;
import com.weng.platform.platform.entity.Column;
import com.weng.platform.platform.mapper.ColumnMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;


/**
 * 新闻资讯基本信息表 服务实现类
 *
 * @author wenghui
 * @date 2019-11-30 10:48:30
 *
 */ 
@Slf4j
@Service
public class ArticleInfoServiceImpl extends BaseServiceImpl<ArticleInfoMapper, ArticleInfo> implements IArticleInfoService {
	@Autowired
	private ArticleInfoMapper newsMapper;
	@Autowired
	private ArticleContentMapper articleContentMapper;
	@Autowired
	private ColumnMapper columnMapper;


	/**
	 * 分页查询新闻资讯基本信息表
	 * @return
	 */
	@Override
	public IPage<ArticleInfoDetailVo> list(Integer pageNo, Integer pageSize, ArticleInfoReq paramData) {
		IPage<ArticleInfoDetailVo> page = new PageModel<>(pageNo,pageSize);
		List<ArticleInfoDetailVo> list = this.newsMapper.getNewsListPage(page, paramData);
		return page.setRecords(list);
	}


	/**
	 * 获取单个新闻资讯基本信息表
	 * @param id
	 * @return
	 */
	@Override
	public ArticleInfo getById(Serializable id) {
		return super.getById(id);
	}

	@Override
	public ArticleInfoDetailVo getArticleInfoDetail(String id) {

		ArticleInfo news = this.getById(id);
		ArticleContent articleContent = getContentByArticleInfoId(news.getId());
		ArticleInfoDetailVo detail = Convert.convert(ArticleInfoDetailVo.class, news);
		if(articleContent!=null){
			detail.setContent(articleContent.getContent());
		}
		Column column = this.columnMapper.selectById(news.getColumnId());

		if(column != null){
			detail.setColumnName(column.getName());
		}
		return detail;
	}

	/**
	 * 新增单个新闻资讯基本信息表
	 * @param addArticleInfoVo
	 * @return
	 */
	@Override
	@Transactional
	public ArticleInfo add(ArticleInfoEditVo addArticleInfoVo) throws GlobalServiceException {

		ArticleInfo news = Convert.convert(ArticleInfo.class, addArticleInfoVo);
		newsMapper.insert(news);
		ArticleContent articleContent = new ArticleContent();
		articleContent.setContent(addArticleInfoVo.getContent());
		articleContent.setArticleId(news.getId());
		articleContentMapper.insert(articleContent);
		return news;
	}

	/**
	 * 更新单个新闻资讯基本信息表
	 * @param newsEditVo
	 * @return
	 * @throws GlobalServiceException
	 */
	@Override
	@Transactional
	public int update(ArticleInfoEditVo newsEditVo) throws GlobalServiceException {
		ArticleInfo news = Convert.convert(ArticleInfo.class, newsEditVo);
		newsMapper.updateById(news);
		ArticleContent articleContent = getContentByArticleInfoId(news.getId());
		if(articleContent != null){
			articleContent.setContent(newsEditVo.getContent());
			articleContentMapper.updateById(articleContent);
		}
		return 1;
	}

	// 通过新闻Id获取新闻详细内容
	private ArticleContent getContentByArticleInfoId(String newsId){
		QueryWrapper<ArticleContent> articleContentQw = new QueryWrapper<ArticleContent>();
		articleContentQw.eq("article_id", newsId);
		ArticleContent articleContent = this.articleContentMapper.selectOne(articleContentQw);
		return articleContent;
	}


	@Override
	@Transactional
	public ResponseData removeByIds(String ids) {
		String[] newsIds = ids.split(",");
		// 删除新闻详情
		for(String newsId : newsIds){
			QueryWrapper<ArticleContent> articleContentQw = new QueryWrapper<ArticleContent>();
			articleContentQw.eq("article_id", newsId);
			articleContentMapper.delete(articleContentQw);
		}

		boolean flag = this.removeByIds(Arrays.asList(newsIds));
		if (flag) {
			return ResponseData.success();
		} else {
			throw new GlobalServiceException(ResultCodeEnum.DATABASE_OPERATE_EXCEPTION.getCode(), "删除新闻资讯基本信息表：" + Arrays.asList(newsIds).toString() + ResultCodeEnum.DATABASE_OPERATE_EXCEPTION.getMessage());
		}
	}

}
